﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.SystemConfig.Services;
using Web.SystemConfig.DataAccess;
using System.Data;
using DreamBuilder.Common;
using DreamBuilder.Core.Message;
using System.Web;
using DreamBuilder.UI.UserControls;
using System.Web.UI.WebControls;

namespace Web.SystemConfig.Business
{
    //by  刘英男

    public class BUSSystem_Pluralist : BusinessBase, ISystem_Pluralist
    {
        DALSystem_Pluralist dal;


        public BUSSystem_Pluralist()
        {
            dal = new DALSystem_Pluralist();
        }
        public int IsParentID()
        {
            int ob = dal.IsParentID();
            return ob;
        }

        #region left 生成树
        private DataTable dtOrg;

        public DataTable DtOrg
        {
            get
            {
                if (dtOrg == null)
                {
                    dtOrg = dal.GetAllOrgData();
                }
                return dtOrg;
            }
        }
        private DataTable dtUser;
        public DataTable DtUser
        {
            get
            {
                if (dtUser == null)
                {
                    dtUser = dal.GetUserByOrgID();
                }
                return dtUser;
            }
        }
        public string leftUrl = null;
        public string rightUrl = null;
        /// <summary>
        /// 生成领导兼职左侧生成树
        /// </summary> 
        public void CreateTreeLeft(System.Web.UI.WebControls.TreeView tree)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID is null ");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                else
                {
                    DataRow rootDr = arrDr[0];
                    System.Web.UI.WebControls.TreeNode root = new System.Web.UI.WebControls.TreeNode();
                    root.Value = rootDr["ORG_ID"].ToString();
                    //root.Text = "[" + rootDr["ORG_Code"] + "]" + rootDr["ORG_Name"].ToString();
                    root.Text = rootDr["ORG_Name"].ToString();
                    tree.Nodes.Add(root);
                    AddChildNodeUser(root, rootDr["ORG_ID"].ToString());
                    AddChildNodeLeft(root, rootDr["ORG_ID"].ToString());
                }
            }
        }
        private void AddChildNodeLeft(System.Web.UI.WebControls.TreeNode root, string parentID)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID='" + parentID + "'");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                foreach (DataRow dr in arrDr)
                {
                    System.Web.UI.WebControls.TreeNode child = new System.Web.UI.WebControls.TreeNode();
                    child.Value = dr["ORG_ID"].ToString();
                    //child.Text = "[" + dr["ORG_Code"] + "]" + dr["ORG_Name"].ToString();
                    child.Text = dr["ORG_Name"].ToString();
                    root.ChildNodes.Add(child);
                    AddChildNodeUser(child, dr["ORG_ID"].ToString());
                    AddChildNodeLeft(child, dr["ORG_ID"].ToString());
                    
                }
            }
        }

        private void AddChildNodeUser(System.Web.UI.WebControls.TreeNode root, string parentID)
        {
            if (DtUser != null && DtUser.Rows.Count > 0)
            {
                DataRow[] arrDrUser = DtUser.Select("ORG_ID='" + parentID + "'");
                if (arrDrUser == null || arrDrUser.Length == 0)
                {
                    return;
                }
                foreach (DataRow dr in arrDrUser)
                {
                    System.Web.UI.WebControls.TreeNode child = new System.Web.UI.WebControls.TreeNode();
                    child.Value = dr["User_ID"].ToString();
                    child.Text = dr["User_Name"].ToString();
                    child.ImageUrl = "../../Images/OA_OU_Tree_Person.gif";
                    string url = "../SystemConfig/System_PluralistEdit.aspx?User_Id=" + dr["User_ID"].ToString();
                    child.Value = url;
                    child.SelectAction = TreeNodeSelectAction.Select;
                    root.ChildNodes.Add(child);
                }
            }
        }

        #endregion
        #region 生成树
        /// <summary>
        /// 生成领导兼职右侧生成树
        /// </summary> 

        public void CreateTree(System.Web.UI.WebControls.TreeView tree)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID is null ");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                else
                {
                    DataRow rootDr = arrDr[0];
                    System.Web.UI.WebControls.TreeNode root = new System.Web.UI.WebControls.TreeNode();
                    root.Value = rootDr["ORG_ID"].ToString();
                    //root.Text = "[" + rootDr["ORG_Code"] + "]" + rootDr["ORG_Name"].ToString();
                    root.Text = rootDr["ORG_Name"].ToString();
                    rightUrl =rootDr["ORG_ID"].ToString();
                    string url = "../SystemConfig/System_PluralistEdit.aspx?Parent_ID=" + rootDr["ORG_ID"].ToString(); ;
                    root.Value = url;
                    root.SelectAction = TreeNodeSelectAction.Select;
                    tree.Nodes.Add(root);
                    AddChildNode(root, rootDr["ORG_ID"].ToString());
                }
            }

        }

        private void AddChildNode(System.Web.UI.WebControls.TreeNode root, string parentID)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID='" + parentID + "'");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                foreach (DataRow dr in arrDr)
                {
                    System.Web.UI.WebControls.TreeNode child = new System.Web.UI.WebControls.TreeNode();
                    child.Value = dr["ORG_ID"].ToString();
                    //child.Text = "[" + dr["ORG_Code"] + "]" + dr["ORG_Name"].ToString();
                    child.Text = dr["ORG_Name"].ToString();
                    rightUrl = dr["ORG_ID"].ToString();
                   string url = "../SystemConfig/System_PluralistEdit.aspx?Parent_ID=" + dr["ORG_ID"].ToString();
                   child.Value = url;
                   child.SelectAction = TreeNodeSelectAction.Select;
                    root.ChildNodes.Add(child);
                    AddChildNode(child, dr["ORG_ID"].ToString());
                }
            }
        }
        #endregion
        #region 返回所属单位名称，用户名
        /// <summary>
        /// 根据所属单位获取单位名称
        /// </summary>
        /// <param name="orgid">组织单元编号</param>
        /// <returns></returns>
        public string GetNameByOrgId(string orgid)
        { 
            return dal.GetNameByOrgId(orgid);
        }
        /// <summary>
        /// 根据用户序号获取用户名称
        /// </summary>
        /// <param name="number">用户编号</param>
        /// <returns></returns>
        public string GetNameByUserId(string userid)
        {
            return dal.GetNameByUserId(userid);
        }
    #endregion
        #region 单位兼管左侧单位生成树
        public void CreateTreeOrgLeft(TreeView tree)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID is null ");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                else
                {
                    DataRow rootDr = arrDr[0];
                    TreeNode root = new TreeNode();
                    root.Value = rootDr["ORG_ID"].ToString();
                    //root.Text = "[" + rootDr["ORG_Code"] + "]" + rootDr["ORG_Name"].ToString();
                    root.Text = rootDr["ORG_Name"].ToString();
                    string url = "../SystemConfig/System_OrgPluralistEdit.aspx?OrgLeftParent_ID=" + rootDr["ORG_ID"].ToString();
                    root.Value = url;
                    root.SelectAction = TreeNodeSelectAction.Select;
                    tree.Nodes.Add(root);
                    AddChildNodeOrgLeft(root, rootDr["ORG_ID"].ToString());
                }
            }
        }

        private void AddChildNodeOrgLeft(TreeNode root, string parentID)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID='" + parentID + "'");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                foreach (DataRow dr in arrDr)
                {
                    TreeNode child = new TreeNode();
                    child.Value = dr["ORG_ID"].ToString();
                    //child.Text="[" + dr["ORG_Code"] + "]" + dr["ORG_Name"].ToString();
                    child.Text = dr["ORG_Name"].ToString();
                    string url = "../SystemConfig/System_OrgPluralistEdit.aspx?OrgLeftParent_ID=" + dr["ORG_ID"].ToString();
                    child.Value = url;
                    child.SelectAction = TreeNodeSelectAction.Select;
                    root.ChildNodes.Add(child);                  
                    AddChildNodeOrgLeft(child, dr["ORG_ID"].ToString());
                }
            }
        }

        #endregion
        #region 单位兼管右侧单位生成树
        public void CreateTreeOrgRight(TreeView tree)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID is null ");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                else
                {
                    DataRow rootDr = arrDr[0];
                    TreeNode root = new TreeNode();
                    root.Value = rootDr["ORG_ID"].ToString();
                    //root.Text = "[" + rootDr["ORG_Code"] + "]" + rootDr["ORG_Name"].ToString(); ;
                    root.Text = rootDr["ORG_Name"].ToString();
                    string url = "../SystemConfig/System_OrgPluralistEdit.aspx?OrgRightParent_ID=" + rootDr["ORG_ID"].ToString();
                    root.Value = url;
                    root.SelectAction = TreeNodeSelectAction.Select;
                    tree.Nodes.Add(root);
                    
                    AddChildNodeOrgRight(root, rootDr["ORG_ID"].ToString());
                }
            }
        }

        private void AddChildNodeOrgRight(TreeNode root, string parentID)
        {
            if (DtOrg != null && DtOrg.Rows.Count > 0)
            {
                DataRow[] arrDr = DtOrg.Select("Parent_ID='" + parentID + "'");
                if (arrDr == null || arrDr.Length == 0)
                {
                    return;
                }
                foreach (DataRow dr in arrDr)
                {
                    TreeNode child = new TreeNode();
                    child.Value = dr["ORG_ID"].ToString();
                   //child.Text= "[" + dr["ORG_Code"] + "]" + dr["ORG_Name"].ToString();
                    child.Text = dr["ORG_Name"].ToString();
                    string url = "../SystemConfig/System_OrgPluralistEdit.aspx?OrgRightParent_ID=" + dr["ORG_ID"].ToString();
                    child.Value = url;
                    root.SelectAction = TreeNodeSelectAction.Select;
                    root.ChildNodes.Add(child);
                    
                    AddChildNodeOrgRight(child, dr["ORG_ID"].ToString());
                }
            }
        }

        #endregion
        #region add by 刘英男 单位序号
        /// <summary>
        /// 根据用户获取单位序号
        /// </summary>
        /// <param name="useridto">被授权用户</param>
        /// <returns>单位序号</returns>
        public String GetOrgIdByUserId(string useridto)
        {
            string OrgIdto = dal.GetOrgIdByUserId(useridto);
            return OrgIdto;
        }
        #endregion
        #region add by 刘英男 单位序号
        /// <summary>
        /// 插入单位序号
        /// </summary>
        /// <param name="id">被授权主键</param>
        /// <param name="org_id">单位序号</param>
        /// <returns></returns>
        public void insertOrgIdtoAuthority(string id, string org_id)
        {
            dal.insertOrgIdtoAuthority(id, org_id);
        }
        #endregion
        #region add by 刘英男 删除用户授权
        /// <summary>
        /// 删除用户授权
        /// </summary>
        /// <param name="pkValue">被授权主键</param>
        /// <returns></returns>
        public ResultInfo DeleteRoleAuthority(string pkValue)
        {
            ResultInfo result = new ResultInfo();
            result.bSuccess = true;
            try
            {
                dal.DeleteRoleAuthority(pkValue);
            }
            catch (Exception e)
            {
                result.bSuccess = false;
                result.ErrorDesc = e.Message;
            }
            return result;
        }
        #endregion
        public void ModifyUserInfo(string userName, DateTime userTime, string userSignId)
        {
            dal.ModifyUserInfo(userName, userTime, userSignId);
        }
        public void ModifyLastUserInfo(string userName, DateTime userTime, string userSignId)
        {
            dal.ModifyLastUserInfo(userName, userTime, userSignId);
        }
        public void modifyUserWord(string pkvalue, string userName)
        {
            dal.modifyUserWordInfo(pkvalue,userName);
        }
        public void ModifyOrgPluralistInfo(string userName, DateTime userTime, string userSignId)
        {
            dal.ModifyOrgPluralistInfo(userName, userTime, userSignId);
        }
        public void ModifyLastOrgPluralistInfo(string userName, DateTime userTime, string userSignId)
        {
            dal.ModifyLastOrgPluralistInfo(userName, userTime, userSignId);
        }
        public void ModifyPluralistInfo(string userName, DateTime userTime, string userSignId)
        {
            dal.ModifyPluralistInfo(userName, userTime, userSignId);
        }
        public void ModifyLastPluralistInfo(string userName, DateTime userTime, string userSignId)
        {
            dal.ModifyLastPluralistInfo(userName, userTime, userSignId);
        }
        public void UpdateUserAuthority(string userfrom, string userto, DateTime userTime)
        {
            DataTable dtAuthority = dal.GetUserAuthorityInfo(userfrom, userto);
            if (dtAuthority != null && dtAuthority.Rows.Count > 0)
            {
                string userLoginTimes = dtAuthority.Rows[0]["LoginTimes"].ToString();
                if ( String.IsNullOrEmpty(userLoginTimes))
                {
                    dal.UserAuthorityLoginTimesInfo(userfrom, userto);
                }
            }
            dal.UpdateUserAuthorityInfo(userfrom, userto, userTime);
        }
        public bool GetUserAuthority(string userfrom, string userto)
        {
            DataTable dtAuthority = dal.GetUserAuthorityInfo(userfrom, userto);
            if (dtAuthority != null && dtAuthority.Rows.Count > 0)
            {
                return true;
            }
            else { return false; }
        }
        public bool DeleteAuthorityView(string pkValue)
        {
            dal.DeleteAuthorityViewInfo(pkValue);
            return true;
        }
        #region add by 刘英男 20111102 职务名称
        /// <summary>
        /// 根据主键获取职务名称
        /// </summary>
        /// <param name="pkValue">主键</param>
        /// <returns>pkName</returns>
        public String GetpkNameByPositionpkValue(string pkValue)
        {
            string pkName = dal.GetpkNameByPositionpkValueInfo(pkValue);
            return pkName;
        }
        #endregion
        public DataTable GetAuthority(string pkValue)
        {
            DataTable dtAuthotity=dal.GetAuthorityInfo(pkValue);
            return dtAuthotity;
        }
        public DataTable GetOrgPlural(string pkValue)
        {
            DataTable dtOrgPlural = dal.GetOrgPluralInfo(pkValue);
            return dtOrgPlural;
        }
        public DataTable GetPluralist(string pkValue)
        {
            DataTable dtPluralist = dal.GetPluralistInfo(pkValue);
            return dtPluralist;
        }
        public DataTable GetSearchKeyWord(string pkValue)
        {
            DataTable dtSearchKeyWord = dal.GetSearchKeyWordInfo(pkValue);
            return dtSearchKeyWord;
        }
        public DataTable GetSignOptionList(string pkValue)
        {
            DataTable dtSignOptionList=dal.GetSignOptionListInfo(pkValue);
            return dtSignOptionList;
        }
        #region add by lyn 检查职务是否被用户占用20120108
        /// <summary>
        ///  检查职务是否被用户占用
        /// </summary>
        /// <param name="pkValue">职务id</param>
        /// <returns>数量</returns>
        public int IsPositionID(string pkValue)
        {
            int ob = dal.IsPositionID(pkValue);
            return ob;
        }
        #endregion
        #region 验证领导兼职，单位兼管 是否有重复数据 add by lyn 20120321
        /// <summary>
        /// 验证领导兼职，单位兼管 是否有重复数据
        /// </summary>
        /// <param name="tableName">数据库表名</param>
        /// <param name="orgid">数据库中单位id</param>
        /// <param name="userid">用户id</param>
        /// <param name="pkOrgId">数据库中时间</param>
        /// <param name="pkUserId">当前时间</param>
        /// <param name="primarykey">数据库主键</param>
        /// <param name="pkValue">主键</param>
        /// <returns></returns>
        public bool getPluralistUserOrgInfo(string tableName, string orgid, string userid, string pkOrgId, string pkUserId)
        {
            DataTable dtPluralist = dal.getPluralistUserOrgInfo(tableName, orgid, userid, pkOrgId, pkUserId);
            if (dtPluralist != null && dtPluralist.Rows.Count > 0)
            {
                return true;
            }
            else
            { 
                return false;
            }           
        }
        #endregion
    }
    }
